package practice3;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

public class UserQueryDAO{
	
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		User u1 = new User();
		u1.setAccount("wdc");
		u1.setUsername("王大锤");
		u1.setPassword("111");
		
		User u2 = new User();
		u2.setAccount("qhs");
		u2.setUsername("奇衡三");
		u2.setPassword("222");
		System.out.println(" >>> 持久化  <<<");
		session.save(u1);
		session.save(u2);
		System.out.println("\n");
		tx.commit();
		session.close();

		Session qSession = HibernateUtil.getSessionFactory().openSession();
		System.out.println(" >>> HQL全表查询：  <<<");
		List<User> users = qSession.createQuery("from User u").list();
		for (Iterator<User> iter = users.iterator(); iter.hasNext();) {
			User user = iter.next();
			System.out.println("用户名:"+user.getUsername());
			System.out.println("账号:"+user.getAccount());
		}
		
		System.out.println(" >>> HQL条件查询：  <<<");
		Query q = qSession.createQuery("from User u where username=:name");
        q.setParameter("name", "王大锤");
        User wdc = (User)q.list().get(0);
		System.out.println("用户名:"+wdc.getUsername());
		System.out.println("账号:"+wdc.getAccount());
		
		System.out.println(" >>> get查询：  <<<");
		User getUser = (User) qSession.get(User.class, users.get(0).getId());
		System.out.println("用户名:"+getUser.getUsername());
		System.out.println("\n");
		User getUser1 = (User) qSession.get(User.class, 1234L);//不存在的ID
		System.out.println("get不存在的用户:"+getUser1);
		System.out.println("\n");
		
		
		System.out.println(" >>> load查询：  <<<");
		User loadUser = (User) qSession.load(User.class, users.get(0).getId());
		System.out.println("用户名:"+loadUser.getUsername());
		User loadUser1 = (User) qSession.load(User.class, 5678L);//不存在的ID
		try {
			System.out.println("load不存在的用户:"+loadUser1);
		} catch (Exception e) {
			System.out.println("捕获load异常:"+e.getMessage());
		}

		System.out.println(" >>> Criteria查询：  <<<");
		Criteria criteria = qSession.createCriteria(User.class);
		criteria.add(Restrictions.like("account", "%w%"));//设定条件
		List<User> ulist = criteria.list();
		System.out.println("用户名:"+ulist.get(0).getUsername());
		
		System.out.println(" >>> 本地SqL查询：  <<<");
		User gin =  (User) 
				qSession.createSQLQuery("select * from User u where username=:name")
		        .addEntity(User.class)
		        .setParameter("name", "奇衡三")
		        .uniqueResult();
		System.out.println("用户名:"+gin.getUsername());
		System.out.println("账号:"+gin.getAccount());
		
		System.out.println(" >>> 删除：  <<<");
		Transaction tx2 = qSession.beginTransaction();
		qSession.delete(gin);
		tx2.commit();
		System.out.println("用户数："+qSession.createCriteria(User.class).list().size());
		qSession.close();
	}
}
